<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="归属人" prop="ascriptionUser">
        <el-input
          v-model="queryParams.ascriptionUser"
          placeholder="请输入归属人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用类型" prop="feeType">
        <el-select
          v-model="queryParams.feeType"
          placeholder="请选择费用类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.crm_fixed_fee_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="费用归属" prop="feeAscription">
        <el-input
          v-model="queryParams.feeAscription"
          placeholder="请输入费用归属"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属部门" prop="ascriptionDept">
        <el-input
          v-model="queryParams.ascriptionDept"
          placeholder="请输入归属部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属组" prop="ascriptionGroup">
        <el-input
          v-model="queryParams.ascriptionGroup"
          placeholder="请输入归属组"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['erp:FixedFee:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['erp:FixedFee:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['erp:FixedFee:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['erp:FixedFee:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="FixedFeeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="归属人"
        align="center"
        prop="ascriptionUserName"
      />
      <el-table-column label="费用类型" align="center" prop="feeType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.crm_fixed_fee_type"
            :value="scope.row.feeType"
          />
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="费用归属" align="center" prop="feeAscription">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.crm_fixed_fee_ascription"
            :value="scope.row.feeAscription"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="归属部门" align="center" prop="ascriptionDeptName">
        <template slot-scope="scope">
          <div v-if="scope.row.ascriptionDeptName">{{scope.row.ascriptionDeptName}}</div>
          <label v-else>/</label>
        </template>
      </el-table-column> -->
      <el-table-column label="归属源" align="center" prop="ascriptionGroup">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.crm_group"
            :value="scope.row.ascriptionGroup"
            v-if="scope.row.ascriptionGroup"
          />
          <div v-if="scope.row.ascriptionDeptName">
            {{ scope.row.ascriptionDeptName }}
          </div>
        </template>
      </el-table-column>
       <el-table-column label="所属年月" align="center" prop="month">
        <template slot-scope="scope">
            {{ parseTime(scope.row.month, '{y}-{m}-{d}') }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['erp:FixedFee:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['erp:FixedFee:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改固定费用支出对话框 -->
    <el-drawer
      ref="drawer"
      :title="title"
      direction="rtl"
      :visible.sync="open"
      size="80%"
      append-to-body
      :wrapperClosable="false"
    >
      <el-card
        class="box-card"
        style="width: 96%; margin-left: 20px; min-height: 90vh"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="归属人" prop="ascriptionUser">
                <el-select
                  v-model="form.ascriptionUser"
                  style="width: 100%"
                  placeholder="请选择归属组"
                  filterable
                >
                  <el-option
                    v-for="user in userList"
                    :key="user.userId"
                    :label="user.userName"
                    :value="user.userId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="费用类型" prop="feeType">
                <el-select
                  v-model="form.feeType"
                  placeholder="请选择费用类型"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in dict.type.crm_fixed_fee_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="费用归属" prop="feeAscription">
                <el-select
                  v-model="form.feeAscription"
                  @change="changeFeeAscription"
                  style="width: 100%"
                  placeholder="请选择费用归属"
                >
                  <el-option
                    v-for="dict in dict.type.crm_fixed_fee_ascription"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.feeAscription === '0'">
              <el-form-item label="归属部门" prop="ascriptionDept">
                <treeselect
                  v-model="form.ascriptionDept"
                  :options="deptOptions"
                  :show-count="true"
                  placeholder="请选择归属部门"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.feeAscription === '1'">
              <el-form-item label="归属组" prop="ascriptionGroup">
                <el-select
                  v-model="form.ascriptionGroup"
                  style="width: 100%"
                  placeholder="请选择归属组"
                >
                  <el-option
                    v-for="dict in dict.type.crm_group"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="金额" prop="amount">
                <el-input
                  v-model="form.amount"
                  placeholder="请输入金额"
                  type="number"
                >
                  <template slot="append">{{
                    digitUppercase(form.amount)
                  }}</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属年月" prop="month">
                <el-date-picker
                  v-model="form.month"
                  type="month"
                  placeholder="选择月"
                  value-format="yyyy-MM-dd"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input
                  v-model="form.remark"
                  placeholder="请输入备注"
                  type="textarea"
                  :rows="3"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import {
  listFixedFee,
  getFixedFee,
  delFixedFee,
  addFixedFee,
  updateFixedFee,
} from "@/api/erp/FixedFee";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import { deptTreeSelect, getUserListByStatus } from "@/api/system/user";

export default {
  name: "FixedFee",
  dicts: ["crm_fixed_fee_type", "crm_fixed_fee_ascription", "crm_group"],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 固定费用支出表格数据
      FixedFeeList: [],
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ascriptionUser: null,
        feeType: null,
        feeAscription: null,
        ascriptionDept: null,
        ascriptionGroup: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ascriptionUser: [
          { required: true, message: "归属人不能为空", trigger: "blur" },
        ],
        feeType: [
          { required: true, message: "费用类型不能为空", trigger: "change" },
        ],
        feeAscription: [
          { required: true, message: "费用归属不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "归属人不能为空", trigger: "blur" },
        ],
        delFlag: [
          { required: true, message: "删除标记不能为空", trigger: "blur" },
        ],
        amount: [
          { required: true, message: "金额不可以为空", trigger: "blur" },
        ],
        month: [
          { required: true, message: "所属年月不可以为空", trigger: "change" },
        ],
        ascriptionGroup: [
          { required: true, message: "归属组不可以为空", trigger: "change" },
        ],
        ascriptionDept: [
          { required: true, message: "归属部门不可以为空", trigger: "change" },
        ]
      },
    };
  },
  created() {
    this.getList();
    this.getUserListByStatus();
    this.getDeptTree();
  },
  methods: {
    changeFeeAscription(val) {
      if (val === "0") {
        this.form.ascriptionGroup = null;
      } else if (val === "1") {
        this.form.ascriptionDept = null;
      }
    },
    getUserListByStatus() {
      getUserListByStatus({ status: "0" }).then((res) => {
        this.userList = res.data;
      });
    },
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询固定费用支出列表 */
    getList() {
      this.loading = true;
      listFixedFee(this.queryParams).then((response) => {
        this.FixedFeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fixedFeeId: null,
        ascriptionUser: null,
        feeType: null,
        feeAscription: "0",
        ascriptionDept: null,
        ascriptionGroup: null,
        userId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.fixedFeeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加固定费用支出";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fixedFeeId = row.fixedFeeId || this.ids;
      getFixedFee(fixedFeeId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改固定费用支出";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.fixedFeeId != null) {
            updateFixedFee(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFixedFee(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fixedFeeIds = row.fixedFeeId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除固定费用支出编号为"' + fixedFeeIds + '"的数据项？'
        )
        .then(function () {
          return delFixedFee(fixedFeeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "erp/FixedFee/export",
        {
          ...this.queryParams,
        },
        `FixedFee_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
